<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Util\String;
use Think\Page;
use Think\Upload;
	class PushDataController extends CommonController{
		var $catelist = array(
				'player'=>'大图|player',
				'wsq-img'=>'微社区图片|wsq-img',
				'wsq-list'=>'微社区列表|wsq-list',
				'travel-img'=>'旅游图|travel-img',
				'travel-list'=>'旅游列表|travel-list',
				'home'=>'家居|home',
				'education'=>'教育|education',
				'life'=>'养生|life',
				'food'=>'美食|food',
				'act-join-list'=>'活动报名列表|act-join-list',
				'act-join-img'=>'活动报名图|act-join-img',
				'act-pre'=>'活动预告|act-pre',
				'act-review-list'=>'活动回顾列表|act-review-list',
				'act-review-img'=>'活动回顾图|act-review-img',
				'index-left-ad'=>'左侧广告|index-left-ad',
				'index-right-ad'=>'右侧广告|index-right-ad',
				'left-ad-a'=>'左栏广告1|left-ad-a',
				'left-ad-b'=>'左栏广告2|left-ad-b'
			);

		public function index(){
			
			$push = D('PushData'); // 实例化User对象
			
			$map = array(); 
    		$parameter = array();
			
			$cateid = $_GET['flag'];
			$title = $_GET['title'];
			if (!$title == NULL){
				$map['title'] = array('like','%'.$title.'%'); 
				$parameter['title'] = $title;
				$this->assign('title',$title);
			}
	    	if (!$cateid == NULL){
				$map['flag'] = array('eq',$cateid); 
				$parameter['flag'] = $cateid;
				$this->assign('flag',$cateid);
			}
			$count      = $push-> where($map)->count();// 查询满足要求的总记录数
			$Page       = new Page($count,9,$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $push-> relation(true)-> where($map)->limit($Page->firstRow.','.$Page->listRows)->order('created_at desc')->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			
			$this->assign('flaglist',$this->catelist);
			
			$this->display('PushData/index');
		}
		
		
		public function del(){
			$m=M('PushData');
			$id=$_GET['id'];
			$count=$m->delete($id);
			if($count>0){
				$this->show('del');
			}else{
				$this->show('error');
			}
		}
		
		public function lock(){
			$m=M('PushData');
			$id=$_GET['id'];
			$sta = $m-> where('id='.$id)->getField('status');
			if($sta>0){
				$m ->where('id='.$id)->setField('status','0');
				$this->show('unlock');
			}else{
				$m ->where('id='.$id)->setField('status','1');
				$this->show('lock');
			}
		}
		

		/*
		 *	显示修改页面
		 * */
		public function modify(){
			
			$id=$_GET['id'];
			$m=M('PushData');
			$arr=$m->find($id);
			$this->assign('data',$arr);
			$this->assign('flaglist',$this->catelist);
			
			$this->display('PushData/edit');
		}
		
		public function update(){
			
			
			$upload = new \Think\Upload();   // 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =      '/images/'; // 设置附件上传目录
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES['fileselect']);
			$m=D('PushData');
			
			if(!$info) {// 上传错误提示错误信息
				unset($_POST['img']);
			}else{// 上传成功 获取上传文件信息
			    $_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
				
				$imginfo = $m-> where('id='.$_POST['id'])->getField('img');
				//删除 原来的 图片 
				if(is_file(__ROOT__.$imginfo)) {
					//echo "删除 员图片 ";
					unlink(__ROOT__.$imginfo);
				}
			}
			
			
			if(!$m->create()){
				$this->error($m->getError());
			}
			
			$idNum=$m->save();
			if($idNum>0){
				$this->success('修改成功','index');
			}else{
				$this->error('数据修改失败');
			}
		}
		
		
	/*
		 * 添加页面
		 * */
		public function add(){
    		$this->assign('flaglist',$this->catelist);
			
			$this->display('PushData/add');
		}
		public function create(){
			
			$upload = new \Think\Upload();   // 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =      '/images/'; // 设置附件上传目录
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES['fileselect']);
			if(!$info) {// 上传错误提示错误信息
			    $_POST['img'] = '';
			}else{// 上传成功 获取上传文件信息
			    $_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
			}
			
			$m=D('PushData');
			if(!$m->create()){
				$this->error($m->getError());
			}
			
			$idNum=$m->add();
			if($idNum>0){
				$this->success('添加推送成功','index');
			}else{
				$this->error('数据添加失败');
			}
			
		}
	}
?>
